﻿// Step 1. Delcare namespace and dependencies.
var myApp = angular.module("myApp", ["ngRoute"]);
// Step 2. Build Factory
myApp.facotry("Cartoons", function () {
    return [];
});

// Step 3. Build Route
// $ is a angular syntex 
myApp.config(function ($routeProvider) {
	$routeProvider
        .when("/",
              {
        templateUrl: "views/Home.html",
        controller: "Home"
              })
		.when("/Home",
				{
				    templateUrl: "views/Home.html",
				    controller: "Home"
				})
		.when("/Add",
				{
				    templateUrl: "views/Add.html",
				    controller: "AddController"
				})
        .when("/Cartoon/:cartoon",
        {
            templateUrl: "views/Details.html",
            controller: "DetailsController"
        })
        .when("/:query",
        {
            templateUrl: "views/Home.html",
            controller: "HomeController"
        })
		.otherwise({
		    templateUrl: "views/Error.html"
		});
});

		myApp.conroller("HomeController",function($scope,$routeParams,Cartoons, $http, $location){
		    $scope.Cartoons = Cartoons;
		    $http.get("https://cartoonsangular.firebaseio.com/,json")
            .success(
            function (data) { 
                for (var id in data) {
                    data[id].id = id;
                    Caroons.push(data[id]);
                }
            })
		    $scope.showDetails = function (carton) {
		        $location.path("/Cartoon/" + Cartoons.indexof(cartoon));
		    };
		    $scope.search = function (item) {
                if ($scope.qeury)
		    }
		});

		myApp.controller("AddController", function ($scope, Cartoons, $http, $location) {
		    $scope.addCartoon = function (name, year, description, picture) {
		        var cartoon = {
		            name: name,
		            year: year,
		            description: description,
		            picture: picture
		        };
		        $http.post("https://cartoonsangular.firebaseio.com/.json", cartoon)
                .success(function () {
                    carton.id = data.name;
                    cartoon.push(cartoon);
                    console.log(Cartoons);
                    $locaiton.path("/")
                });
		    }
		});
		myApp.controller("DetailsControler", function ($scope, Cartoons, $routeParams, $location) {
		    if (Cartoon[$routePerams.cartoon]) {
		        $scope.Cartoon = Cartoons[$routeParams.cartoon];
		    } else {
		        $locaton.path("/");
		    }
		});
        
		myApp.controller("SearchController", function ($scope,$location, $routeParams) {
		    $scope.search = function (query) {
		        $location.path("/" + query);
		    }
		});
		myApp.filter("Snippet",function(){
		    return function (text){
		        return text.slice(0,10) + "...";
		    };
		})